<template>
  <div class="login-container">
    <el-form ref="form" class="myform" :model="form" :rules="rules">
      <h3>iHRM后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          placeholder="请输入用户名"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          show-password
          placeholder="请输入密码"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="isLoading" class="btn" @click="login"
          >登录</el-button
        >
      </el-form-item>
      <div class="mark"><span>还没有账号？立即注册</span></div>
    </el-form>
  </div>
</template>

<script>
// 导入登录接口
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 表单校验规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '密码长度范围在 6 ~ 12 位',
            trigger: 'blur'
          }
        ]
      },
      isLoading: false // 按钮加载状态
    }
  },
  methods: {
    // 使用辅助函数快速获取登录接口的方法
    ...mapActions({
      userLogin: 'user/userlogin'
    }),
    login () {
      // 首先校验输入框数据是否正确
      this.$refs.form.validate(async vaild => {
        if (vaild) {
          try {
            // 验证通过
            this.isLoading = true
            // 调用actions中的方法来发送请求
            await this.userLogin(this.form)
            // 跳转首页前先判断是否因 token 过期导致
            if (this.$route.query._redirect) {
              // 是则跳转原页面
              this.$router.push(this.$route.query._redirect)
            } else {
              // 否则跳转主页
              this.$router.push('/')
            }
            this.$message.success('登录成功')
          } catch (error) {
            console.log(error) // 打印错误信息
          } finally {
            // 无论成功失败都关闭加载动画
            this.isLoading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  background: url(../../assets/common/login.jpg) no-repeat;
  background-position: center;
  .myform {
    width: 520px;
    height: 400px;
    padding: 35px 35px 15px 35px;
    position: absolute;
    top: calc(38% - 200px);
    left: calc(50% - 260px);

    h3 {
      font-size: 46px;
      color: #fff;
      font-weight: 100;
      text-align: center;
      letter-spacing: 4px;
    }
    input {
      height: 47px;
      color: #68b0fe;
      background: #d3e5ff;
      border: 0;
      padding: 15px 5px 12px 40px;
    }
    .btn {
      width: 100%;
      background: #407ffe;
      height: 64px;
      // line-height: 32px;
      font-size: 24px;
      color: #fff;
      border: none;
      margin-bottom: 10px;
    }
    .mark {
      text-align: center;
      font-size: 16px;
      color: #fff;
      span:hover {
        cursor: pointer;
        text-decoration: underline;
      }
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
